package com.hxd.otc.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hxd.otc.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 包  名 :  com.hxd.otc.widget
 * 类  名 :  DealTitleBar
 * 作  者 :  胡庆岭
 * 时  间 :  2018/6/6 14:13
 * 描  述 :  ${交易顶部Title栏}
 */
public class DealTitleBar extends RelativeLayout {

    @BindView(R.id.convert_tv)
    TextView mConvertText;
    @BindView(R.id.queues_cb)
    public CheckBox mQueuesCheck;
    @BindView(R.id.message_count_tv)
    TextView mMessageCountText;
    @BindView(R.id.status_tv)
    TextView mStatusText;
    @BindView(R.id.message_hint_iv)
    ImageView mMessageHintImage;
    @BindView(R.id.parent_rl)
    RelativeLayout mParentGroup;
    private boolean mShowRightStauts;
    private Unbinder mBind;
    private OnLeftClickListener onLeftClickListener;

    public void setOnCheckTitleListener(OnCheckTitleListener onCheckTitleListener) {
        this.onCheckTitleListener = onCheckTitleListener;
    }

    public void setOnLeftClickListener(OnLeftClickListener onLeftClickListener) {
        this.onLeftClickListener = onLeftClickListener;
    }

    private OnCheckTitleListener onCheckTitleListener;

    public DealTitleBar(Context context) {
        this(context, null);
    }

    public DealTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
        initAttribute(attrs);
        initEvent();

    }

    private void initEvent() {
        mQueuesCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (onCheckTitleListener != null) {
                    onCheckTitleListener.onCheckChanged(isChecked);
                }
            }
        });
        mStatusText.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onCheckTitleListener != null) {
                    onCheckTitleListener.onRightClick(v);
                }
            }
        });
        mConvertText.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onLeftClickListener != null) {
                    onLeftClickListener.onClick();
                }
            }
        });
    }

    private void initView() {
        View inflate = LayoutInflater.from(getContext()).inflate(R.layout.weight_deal_title_bar, this);
        mBind = ButterKnife.bind(this, inflate);

    }

    private void initAttribute(AttributeSet attrs) {
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.DealTitleBar);
        if (typedArray != null && typedArray.getIndexCount() > 0) {
            mShowRightStauts = typedArray.getBoolean(R.styleable.DealTitleBar_deal_show_right_status, true);
            if (typedArray.getBoolean(R.styleable.DealTitleBar_deal_show_check_icon, true)) {
                mQueuesCheck.setCompoundDrawablesWithIntrinsicBounds(null, null, ContextCompat.getDrawable(getContext(), R.drawable.selector_deal_tab_view), null);
            } else {
                mQueuesCheck.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
            }
            mMessageCountText.setVisibility(mShowRightStauts ? GONE : VISIBLE);
            mMessageHintImage.setVisibility(mShowRightStauts ? GONE : VISIBLE);
            mStatusText.setVisibility(mShowRightStauts ? VISIBLE : GONE);
            mConvertText.setText(typedArray.getString(R.styleable.DealTitleBar_deal_left_title));
            mConvertText.setTextColor(typedArray.getColor(R.styleable.DealTitleBar_deal_left_text_color, ContextCompat.getColor(getContext(), R.color.color_text2)));
            mStatusText.setText(typedArray.getString(R.styleable.DealTitleBar_deal_status_content));
            mQueuesCheck.setText(typedArray.getString(R.styleable.DealTitleBar_deal_title_content));
            typedArray.recycle();
        }
    }

    /**
     * 设置行情汇率
     *
     * @param convert 行情
     */
    public void setConvert(@NonNull String convert) {
        mConvertText.setText(convert);
    }

    /**
     * 设置交易队
     *
     * @param queuesContent 交易队
     */
    public void setQueuesContent(@NonNull String queuesContent) {
        mQueuesCheck.setText(queuesContent);
    }

    /**
     * 获取交易队
     *
     */
    public String getQueuesContent() {
        return mQueuesCheck.getText().toString().trim();
    }

    @Override
    protected void onDetachedFromWindow() {
        if (mBind != null) {
            mBind.unbind();
        }
        super.onDetachedFromWindow();
    }

    @OnClick(R.id.message_hint_iv)
    public void onViewClicked() {

    }

    public void setMessageCount(@NonNull String count) {
        if (Integer.valueOf(count) <= 0) {
            mMessageCountText.setVisibility(GONE);
        } else {
            mMessageCountText.setVisibility(VISIBLE);
            mMessageCountText.setText(count);
        }
    }

    public interface OnCheckTitleListener {
        void onCheckChanged(boolean isCheck);

        void onRightClick(View v);
    }

    public interface OnLeftClickListener {
        void onClick();
    }

    public void setAllClickStatus(boolean isClick) {
        for (int i = 0; i < mParentGroup.getChildCount(); i++) {
            mParentGroup.getChildAt(i).setClickable(!isClick);
            mParentGroup.getChildAt(i).setEnabled(!isClick);
        }

    }

    public void setDealData(String transactionPair, boolean isCheck) {
        mQueuesCheck.setText(transactionPair);
        mQueuesCheck.setChecked(isCheck);
    }

    public void setCheck(boolean isCheck) {
        mQueuesCheck.setChecked(isCheck);
    }

    public void setDealData(String transactionPair) {
        mQueuesCheck.setText(transactionPair);
    }

    public void setDealData(String quotation, String transactionPair) {
        mQueuesCheck.setText(transactionPair);
        mConvertText.setText(quotation);

    }

    public void setDealData(String quotation, String transactionPair, boolean isCheck) {

        mQueuesCheck.setText(transactionPair);
        mConvertText.setText(getContext().getString(R.string.default_deal_convert, quotation));
        mQueuesCheck.setChecked(isCheck);

    }

}
